package com.pilipili.zeus.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.pilipili.zeus.eduService.entity.EduCourseCollect;
import com.pilipili.zeus.eduService.entity.EduSubject;
import com.pilipili.zeus.eduService.entity.excel.SubjectExcelData;
import com.pilipili.zeus.eduService.entity.subject.SubjectNode;
import com.pilipili.zeus.eduService.listener.ExcelListener;
import com.pilipili.zeus.eduService.mapper.EduSubjectMapper;
import com.pilipili.zeus.eduService.service.EduCourseCollectService;
import com.pilipili.zeus.eduService.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 番剧科目 服务实现类
 *
 * @author Unlimited_Blade_Works
 * @since 2021-05-25
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    @Autowired
    private EduCourseCollectService courseCollectService;
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {

        try {
            //文件输入流,调用方法读取Excel
            InputStream stream = file.getInputStream();
            EasyExcel.read(stream, SubjectExcelData.class,new ExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<SubjectNode> getSubjectTree() {
        List<EduSubject> entities = list();
        //将获取的eduSubject对象数组封装成SubjectNode对象数组
        // BeanUtils.copyProperties:Bean对象的复制和储存
        //collect(Collectors.toList()):实例化对象，填充和合并多个中间结果，以保持可变数据结构的隔离
        List<SubjectNode> subjectNodes = entities.stream().map(eduSubject -> {
            SubjectNode subjectNode = new SubjectNode();
            BeanUtils.copyProperties(eduSubject, subjectNode);
            return subjectNode;
        }).collect(Collectors.toList());

        //filter取得所有parentId为0的数据,也就是一级目录
        //map取得一级目录的下级目录的数据
        return subjectNodes.stream().filter(subjectNode -> subjectNode.getParentId().equals("0")
        ).peek((menu) -> menu.setChildren(getCourseChildrenData(menu, subjectNodes))).collect(Collectors.toList());
    }

    @Override
    public void saveCourseSubjectRelationShip(String courseId, String[] subjectId) {
        List<EduCourseCollect> courseCollectList=new ArrayList<>();
        for (String subject : subjectId) {
            EduCourseCollect courseCollect = new EduCourseCollect();
            courseCollect.setCourseId(courseId);
            courseCollect.setMemberId(subject);
            courseCollectList.add(courseCollect);
        }
        courseCollectService.saveBatch(courseCollectList);
    }

    //获取下级目录的方法，递归实现
    private List<SubjectNode> getCourseChildrenData(SubjectNode root,List<SubjectNode> lists){
        return lists.stream().filter(subjectNode -> subjectNode.getParentId().equals(root.getId())
        ).peek(subjectNode -> subjectNode.setChildren(getCourseChildrenData(subjectNode, lists))).collect(Collectors.toList());
    }


}
